package logic;

import java.awt.Color;

/**
 * @author christ66
 *
 */
public class ChessBoard {

    /*
     * Creates the chessboard. We will be using a 8x8 matrix of Chess Pieces.
     *
     */
    public ChessBoard() {
        whiteKingMoved = blackKingMoved = false;
        whiteARookMoved = blackARookMoved = whiteHRookMoved = blackHRookMoved = false;
        lastMovePawn = false;
        lastMove = 99;
        /*
         * Set up all the pieces on the board for Black and White teams.
         */
        for (int c = 0; c < MAX_COLUMNS; c++) {
            /*
             * Set up all the Pawn Pieces
             */
            board[1][c] = new Pawn(Color.white);
            board[6][c] = new Pawn(Color.black);

            // Set up empty squares
            board[2][c] = new Empty();
            board[3][c] = new Empty();
            board[4][c] = new Empty();
            board[5][c] = new Empty();
        }

        /*
         * Set up all Rooks, Bishops, and Knights for White and Black Teams
         */
        board[0][0] = new Rook(Color.white);
        board[0][1] = new Knight(Color.white);
        board[0][2] = new Bishop(Color.white);

        board[0][7] = new Rook(Color.white);
        board[0][6] = new Knight(Color.white);
        board[0][5] = new Bishop(Color.white);


        board[7][0] = new Rook(Color.black);
        board[7][1] = new Knight(Color.black);
        board[7][2] = new Bishop(Color.black);

        board[7][7] = new Rook(Color.black);
        board[7][6] = new Knight(Color.black);
        board[7][5] = new Bishop(Color.black);

        /*
         * Set the King and Queen up for Black and White teams
         */
        board[7][3] = new Queen(Color.black);
        board[7][4] = new King(Color.black);

        board[0][3] = new Queen(Color.white);
        board[0][4] = new King(Color.white);

        threefold[0] = boardHash();
        threefoldcount[0] = 1;
        boardState[moveCounter] = boardHash();
    }

    @Override
    public String toString() {
        final StringBuilder returnString = new StringBuilder();
        for (int r = 0; r < MAX_ROWS; r++) {
            for (int c = 0; c < MAX_COLUMNS; c++) {
                returnString.append('[').append(board[7 - r][c]).append(']');
            }
            returnString.append("\n");
        }
        return returnString.toString();
    }
    private String boardHash() {
        final StringBuilder rs = new StringBuilder();
        for (int r = 0; r < MAX_ROWS; r++) {
            for (int c = 0; c < MAX_COLUMNS; c++) {
                if (board[7 - r][c].toString().equals(" ")) {
                    rs.append("0");
                } else if (board[7 - r][c].getColor() == Color.black) {
                    rs.append(board[7 - r][c].toString().toLowerCase());
                } else {
                    rs.append(board[7 - r][c].toString().toUpperCase());
                }
            }
        }
        return rs.toString();
    }
    public void movePiece(final int sRank, final int sColumn, final int nRank, final int nColumn) {
        if (board[sRank][sColumn].toString().equals("P")) {
            lastMovePawn = true;
        } else {
            lastMovePawn = false;
        }
        if (!board[nRank][nColumn].toString().equals(" ")) {
            //This is a capture and threefold can be reset
            hashCount = 1;
            threefold = new String[50];
            threefoldcount = new int[50];
            threefold[0] = boardHash();
            threefoldcount[0] = 1;
        }
        board[nRank][nColumn] = board[sRank][sColumn];
        board[sRank][sColumn] = new Empty();
        lastMove = nRank * 10 + nColumn;
        if (sRank == 0 && sColumn == 0) {
            whiteARookMoved = true;
        } else if (sRank == 0 && sColumn == 4) {
            whiteKingMoved = true;
        } else if (sRank == 0 && sColumn == 7) {
            whiteHRookMoved = true;
        } else if (sRank == 7 && sColumn == 0) {
            blackARookMoved = true;
        } else if (sRank == 7 && sColumn == 4) {
            blackKingMoved = true;
        } else if (sRank == 7 && sColumn == 7) {
            blackHRookMoved = true;
        }
        threeFold();
        recordState();
    }
    public void changePiece(final int rank, final int column, final int type, final Color color) {
        switch (type) {
            case 1:
                board[rank][column] = new Queen(color);
                return;
            case 2:
                board[rank][column] = new Rook(color);
                return;
            case 3:
                board[rank][column] = new Bishop(color);
                return;
            case 4:
                board[rank][column] = new Knight(color);
                return;
            case 5:
                board[rank][column] = new Pawn(color);
                return;
            case 6:
                board[rank][column] = new King(color);
                return;
            case 7:
                board[rank][column] = new Empty();
            default:
                return;
        }
    }

    public String pieceAt(final int rank, final int column) {
        return board[rank][column].toString();
    }
    public Color colorAt(final int rank, final int column) {
        return board[rank][column].getColor();
    }
    
    public int[] validMoves(final int rank, final int column) {
        int[] result = new int[30];
        int index = 0;

        for (int x = 0; x < 30; x++) {
            result[x] = 99;
        }

        if (board[rank][column].toString().equals("R")) {
            // <editor-fold defaultstate="collapsed" desc="Rook move logic">
            if (board[rank][column].getColor() == Color.black) {
                //Get moves above
                for (int x = rank + 1; x <= 7; x++) {
                    if (board[x][column].getColor() == null) {
                        result[index++] = x * 10 + column;
                        continue;
                    } else if (board[x][column].getColor() == Color.white) {
                        result[index++] = x * 10 + column;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below
                for (int x = rank - 1; x >= 0; x--) {
                    if (board[x][column].getColor() == null) {
                        result[index++] = x * 10 + column;
                        continue;
                    } else if (board[x][column].getColor() == Color.white) {
                        result[index++] = x * 10 + column;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves left
                for (int x = column - 1; x >= 0; x--) {
                    if (board[rank][x].getColor() == null) {
                        result[index++] = rank * 10 + x;
                        continue;
                    } else if (board[rank][x].getColor() == Color.white) {
                        result[index++] = rank * 10 + x;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves right
                for (int x = column + 1; x <= 7; x++) {
                    if (board[rank][x].getColor() == null) {
                        result[index++] = rank * 10 + x;
                        continue;
                    } else if (board[rank][x].getColor() == Color.white) {
                        result[index++] = rank * 10 + x;
                        break;
                    } else {
                        break;
                    }
                }
            } else if (board[rank][column].getColor() == Color.white) {
                //Get moves above
                for (int x = rank + 1; x <= 7; x++) {
                    if (board[x][column].getColor() == null) {
                        result[index++] = x * 10 + column;
                        continue;
                    } else if (board[x][column].getColor() == Color.black) {
                        result[index++] = x * 10 + column;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below
                for (int x = rank - 1; x >= 0; x--) {
                    if (board[x][column].getColor() == null) {
                        result[index++] = x * 10 + column;
                        continue;
                    } else if (board[x][column].getColor() == Color.black) {
                        result[index++] = x * 10 + column;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves left
                for (int x = column - 1; x >= 0; x--) {
                    if (board[rank][x].getColor() == null) {
                        result[index++] = rank * 10 + x;
                        continue;
                    } else if (board[rank][x].getColor() == Color.black) {
                        result[index++] = rank * 10 + x;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves right
                for (int x = column + 1; x <= 7; x++) {
                    if (board[rank][x].getColor() == null) {
                        result[index++] = rank * 10 + x;
                        continue;
                    } else if (board[rank][x].getColor() == Color.black) {
                        result[index++] = rank * 10 + x;
                        break;
                    } else {
                        break;
                    }
                }
            }
            // </editor-fold>
        } else if (board[rank][column].toString().equals("N")) {
            // <editor-fold defaultstate="collapsed" desc="Knight move logic">
            if (board[rank][column].getColor() == Color.black) {
                if (rank + 2 <= 7) {
                    if (column + 1 <= 7) {
                        if (board[rank + 2][column + 1].getColor() == null || board[rank + 2][column + 1].getColor() == Color.white) {
                            result[index++] = (rank + 2) * 10 + column + 1;
                        }
                    }
                    if (column - 1 >= 0) {
                        if (board[rank + 2][column - 1].getColor() == null || board[rank + 2][column - 1].getColor() == Color.white) {
                            result[index++] = (rank + 2) * 10 + column - 1;
                        }
                    }
                }
                if (column + 2 <= 7) {
                    if (rank + 1 <= 7) {
                        if (board[rank + 1][column + 2].getColor() == null || board[rank + 1][column + 2].getColor() == Color.white) {
                            result[index++] = (rank + 1) * 10 + column + 2;
                        }
                    }
                    if (rank - 1 >= 0) {
                        if (board[rank - 1][column + 2].getColor() == null || board[rank - 1][column + 2].getColor() == Color.white) {
                            result[index++] = (rank - 1) * 10 + column + 2;
                        }
                    }
                }
                if (rank - 2 >= 0) {
                    if (column + 1 <= 7) {
                        if (board[rank - 2][column + 1].getColor() == null || board[rank - 2][column + 1].getColor() == Color.white) {
                            result[index++] = (rank - 2) * 10 + column + 1;
                        }
                    }
                    if (column - 1 >= 0) {
                        if (board[rank - 2][column - 1].getColor() == null || board[rank - 2][column - 1].getColor() == Color.white) {
                            result[index++] = (rank - 2) * 10 + column - 1;
                        }
                    }
                }
                if (column - 2 >= 0) {
                    if (rank + 1 <= 7) {
                        if (board[rank + 1][column - 2].getColor() == null || board[rank + 1][column - 2].getColor() == Color.white) {
                            result[index++] = (rank + 1) * 10 + column - 2;
                        }
                    }
                    if (rank - 1 >= 0) {
                        if (board[rank - 1][column - 2].getColor() == null || board[rank - 1][column - 2].getColor() == Color.white) {
                            result[index++] = (rank - 1) * 10 + column - 2;
                        }
                    }
                }
            } else if (board[rank][column].getColor() == Color.white) {
                if (rank + 2 <= 7) {
                    if (column + 1 <= 7) {
                        if (board[rank + 2][column + 1].getColor() == null || board[rank + 2][column + 1].getColor() == Color.black) {
                            result[index++] = (rank + 2) * 10 + column + 1;
                        }
                    }
                    if (column - 1 >= 0) {
                        if (board[rank + 2][column - 1].getColor() == null || board[rank + 2][column - 1].getColor() == Color.black) {
                            result[index++] = (rank + 2) * 10 + column - 1;
                        }
                    }
                }
                if (column + 2 <= 7) {
                    if (rank + 1 <= 7) {
                        if (board[rank + 1][column + 2].getColor() == null || board[rank + 1][column + 2].getColor() == Color.black) {
                            result[index++] = (rank + 1) * 10 + column + 2;
                        }
                    }
                    if (rank - 1 >= 0) {
                        if (board[rank - 1][column + 2].getColor() == null || board[rank - 1][column + 2].getColor() == Color.black) {
                            result[index++] = (rank - 1) * 10 + column + 2;
                        }
                    }
                }
                if (rank - 2 >= 0) {
                    if (column + 1 <= 7) {
                        if (board[rank - 2][column + 1].getColor() == null || board[rank - 2][column + 1].getColor() == Color.black) {
                            result[index++] = (rank - 2) * 10 + column + 1;
                        }
                    }
                    if (column - 1 >= 0) {
                        if (board[rank - 2][column - 1].getColor() == null || board[rank - 2][column - 1].getColor() == Color.black) {
                            result[index++] = (rank - 2) * 10 + column - 1;
                        }
                    }
                }
                if (column - 2 >= 0) {
                    if (rank + 1 <= 7) {
                        if (board[rank + 1][column - 2].getColor() == null || board[rank + 1][column - 2].getColor() == Color.black) {
                            result[index++] = (rank + 1) * 10 + column - 2;
                        }
                    }
                    if (rank - 1 >= 0) {
                        if (board[rank - 1][column - 2].getColor() == null || board[rank - 1][column - 2].getColor() == Color.black) {
                            result[index++] = (rank - 1) * 10 + column - 2;
                        }
                    }
                }
            }
            // </editor-fold>
        } else if (board[rank][column].toString().equals("B")) {
            // <editor-fold defaultstate="collapsed" desc="Rook move logic">
            int r, c;
            if (board[rank][column].getColor() == Color.black) {
                //Get moves above-right
                r = rank + 1;
                c = column + 1;
                while (r <= 7 && c <= 7) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r++ * 10 + c++;
                        continue;
                    } else if (board[r][c].getColor() == Color.white) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below-left
                r = rank - 1;
                c = column - 1;
                while (r >= 0 && c >= 0) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r-- * 10 + c--;
                        continue;
                    } else if (board[r][c].getColor() == Color.white) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves above-left
                r = rank + 1;
                c = column - 1;
                while (r <= 7 && c >= 0) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r++ * 10 + c--;
                        continue;
                    } else if (board[r][c].getColor() == Color.white) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below-right
                r = rank - 1;
                c = column + 1;
                while (r >= 0 && c <= 7) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r-- * 10 + c++;
                        continue;
                    } else if (board[r][c].getColor() == Color.white) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
            } else if (board[rank][column].getColor() == Color.white) {
                //Get moves above-right
                r = rank + 1;
                c = column + 1;
                while (r <= 7 && c <= 7) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r++ * 10 + c++;
                        continue;
                    } else if (board[r][c].getColor() == Color.black) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below-left
                r = rank - 1;
                c = column - 1;
                while (r >= 0 && c >= 0) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r-- * 10 + c--;
                        continue;
                    } else if (board[r][c].getColor() == Color.black) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves above-left
                r = rank + 1;
                c = column - 1;
                while (r <= 7 && c >= 0) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r++ * 10 + c--;
                        continue;
                    } else if (board[r][c].getColor() == Color.black) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below-right
                r = rank - 1;
                c = column + 1;
                while (r >= 0 && c <= 7) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r-- * 10 + c++;
                        continue;
                    } else if (board[r][c].getColor() == Color.black) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
            }
            // </editor-fold>
        } else if (board[rank][column].toString().equals("Q")) {
            // <editor-fold defaultstate="collapsed" desc="Queen move logic">
            if (board[rank][column].getColor() == Color.black) {
                //Get moves above
                for (int x = rank + 1; x <= 7; x++) {
                    if (board[x][column].getColor() == null) {
                        result[index++] = x * 10 + column;
                        continue;
                    } else if (board[x][column].getColor() == Color.white) {
                        result[index++] = x * 10 + column;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below
                for (int x = rank - 1; x >= 0; x--) {
                    if (board[x][column].getColor() == null) {
                        result[index++] = x * 10 + column;
                        continue;
                    } else if (board[x][column].getColor() == Color.white) {
                        result[index++] = x * 10 + column;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves left
                for (int x = column - 1; x >= 0; x--) {
                    if (board[rank][x].getColor() == null) {
                        result[index++] = rank * 10 + x;
                        continue;
                    } else if (board[rank][x].getColor() == Color.white) {
                        result[index++] = rank * 10 + x;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves right
                for (int x = column + 1; x <= 7; x++) {
                    if (board[rank][x].getColor() == null) {
                        result[index++] = rank * 10 + x;
                        continue;
                    } else if (board[rank][x].getColor() == Color.white) {
                        result[index++] = rank * 10 + x;
                        break;
                    } else {
                        break;
                    }
                }
            } else if (board[rank][column].getColor() == Color.white) {
                //Get moves above
                for (int x = rank + 1; x <= 7; x++) {
                    if (board[x][column].getColor() == null) {
                        result[index++] = x * 10 + column;
                        continue;
                    } else if (board[x][column].getColor() == Color.black) {
                        result[index++] = x * 10 + column;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below
                for (int x = rank - 1; x >= 0; x--) {
                    if (board[x][column].getColor() == null) {
                        result[index++] = x * 10 + column;
                        continue;
                    } else if (board[x][column].getColor() == Color.black) {
                        result[index++] = x * 10 + column;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves left
                for (int x = column - 1; x >= 0; x--) {
                    if (board[rank][x].getColor() == null) {
                        result[index++] = rank * 10 + x;
                        continue;
                    } else if (board[rank][x].getColor() == Color.black) {
                        result[index++] = rank * 10 + x;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves right
                for (int x = column + 1; x <= 7; x++) {
                    if (board[rank][x].getColor() == null) {
                        result[index++] = rank * 10 + x;
                        continue;
                    } else if (board[rank][x].getColor() == Color.black) {
                        result[index++] = rank * 10 + x;
                        break;
                    } else {
                        break;
                    }
                }
            }
            int r, c;
            if (board[rank][column].getColor() == Color.black) {
                //Get moves above-right
                r = rank + 1;
                c = column + 1;
                while (r <= 7 && c <= 7) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r++ * 10 + c++;
                        continue;
                    } else if (board[r][c].getColor() == Color.white) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below-left
                r = rank - 1;
                c = column - 1;
                while (r >= 0 && c >= 0) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r-- * 10 + c--;
                        continue;
                    } else if (board[r][c].getColor() == Color.white) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves above-left
                r = rank + 1;
                c = column - 1;
                while (r <= 7 && c >= 0) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r++ * 10 + c--;
                        continue;
                    } else if (board[r][c].getColor() == Color.white) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below-right
                r = rank - 1;
                c = column + 1;
                while (r >= 0 && c <= 7) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r-- * 10 + c++;
                        continue;
                    } else if (board[r][c].getColor() == Color.white) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
            } else if (board[rank][column].getColor() == Color.white) {
                //Get moves above-right
                r = rank + 1;
                c = column + 1;
                while (r <= 7 && c <= 7) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r++ * 10 + c++;
                        continue;
                    } else if (board[r][c].getColor() == Color.black) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below-left
                r = rank - 1;
                c = column - 1;
                while (r >= 0 && c >= 0) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r-- * 10 + c--;
                        continue;
                    } else if (board[r][c].getColor() == Color.black) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves above-left
                r = rank + 1;
                c = column - 1;
                while (r <= 7 && c >= 0) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r++ * 10 + c--;
                        continue;
                    } else if (board[r][c].getColor() == Color.black) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
                //Get moves below-right
                r = rank - 1;
                c = column + 1;
                while (r >= 0 && c <= 7) {
                    if (board[r][c].getColor() == null) {
                        result[index++] = r-- * 10 + c++;
                        continue;
                    } else if (board[r][c].getColor() == Color.black) {
                        result[index++] = r * 10 + c;
                        break;
                    } else {
                        break;
                    }
                }
            }
            // </editor-fold>
        } else if (board[rank][column].toString().equals("K")) {
            // <editor-fold defaultstate="collapsed" desc="King move logic">
            if (board[rank][column].getColor() == Color.black) {

                //if king is in check, do not do

                if (!blackKingMoved && !checked) {
                    //King side castle
                    if (board[7][5].getColor() == null && board[7][6].getColor() == null && !blackHRookMoved) {
                        //f8 and g8 clear and H rook has not moved
                        result[index++] = 76;
                    }

                    //Queen side castle
                    if (board[7][3].getColor() == null && board[7][2].getColor() == null && board[7][1].getColor() == null && !blackARookMoved) {
                        //d8, c8, b8 clear and A rook not has not moved
                        result[index++] = 72;
                    }
                }
                if (rank > 0 && column > 0 && rank < 7 && column < 7) {
                    //Check all sides
                    if (board[rank - 1][column + 1].getColor() == null || board[rank - 1][column + 1].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column + 1;
                    }
                    if (board[rank - 1][column].getColor() == null || board[rank - 1][column].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column;
                    }
                    if (board[rank - 1][column - 1].getColor() == null || board[rank - 1][column - 1].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column - 1;
                    }

                    if (board[rank][column + 1].getColor() == null || board[rank][column + 1].getColor() == Color.white) {
                        result[index++] = rank * 10 + column + 1;
                    }
                    if (board[rank][column - 1].getColor() == null || board[rank][column - 1].getColor() == Color.white) {
                        result[index++] = rank * 10 + column - 1;
                    }

                    if (board[rank + 1][column + 1].getColor() == null || board[rank + 1][column + 1].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column + 1;
                    }
                    if (board[rank + 1][column].getColor() == null || board[rank + 1][column].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column;
                    }
                    if (board[rank + 1][column - 1].getColor() == null || board[rank + 1][column - 1].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column - 1;
                    }
                } else if (rank == 0 && column > 0 && column < 7) {
                    //Check l,ul,u,ur,r
                    if (board[rank + 1][column + 1].getColor() == null || board[rank + 1][column + 1].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column + 1;
                    }
                    if (board[rank + 1][column].getColor() == null || board[rank + 1][column].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column;
                    }
                    if (board[rank + 1][column - 1].getColor() == null || board[rank + 1][column - 1].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column - 1;
                    }

                    if (board[rank][column + 1].getColor() == null || board[rank][column + 1].getColor() == Color.white) {
                        result[index++] = rank * 10 + column + 1;
                    }
                    if (board[rank][column - 1].getColor() == null || board[rank][column - 1].getColor() == Color.white) {
                        result[index++] = rank * 10 + column - 1;
                    }
                } else if (rank == 7 && column > 0 && column < 7) {
                    //check l, bl, b, br, r
                    if (board[rank - 1][column + 1].getColor() == null || board[rank - 1][column + 1].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column + 1;
                    }
                    if (board[rank - 1][column].getColor() == null || board[rank - 1][column].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column;
                    }
                    if (board[rank - 1][column - 1].getColor() == null || board[rank - 1][column - 1].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column - 1;
                    }

                    if (board[rank][column + 1].getColor() == null || board[rank][column + 1].getColor() == Color.white) {
                        result[index++] = rank * 10 + column + 1;
                    }
                    if (board[rank][column - 1].getColor() == null || board[rank][column - 1].getColor() == Color.white) {
                        result[index++] = rank * 10 + column - 1;
                    }
                } else if (column == 0 && rank > 0 && rank < 7) {
                    //check u, ur, r, br, b
                    if (board[rank + 1][column + 1].getColor() == null || board[rank + 1][column + 1].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column + 1;
                    }
                    if (board[rank + 1][column].getColor() == null || board[rank + 1][column].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column;
                    }

                    if (board[rank][column + 1].getColor() == null || board[rank][column + 1].getColor() == Color.white) {
                        result[index++] = rank * 10 + column + 1;
                    }

                    if (board[rank - 1][column + 1].getColor() == null || board[rank - 1][column + 1].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column + 1;
                    }
                    if (board[rank - 1][column].getColor() == null || board[rank - 1][column].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column;
                    }
                } else if (column == 7 && rank > 0 && rank < 7) {
                    //check u, ul, l, bl, b
                    if (board[rank + 1][column - 1].getColor() == null || board[rank + 1][column - 1].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column - 1;
                    }
                    if (board[rank + 1][column].getColor() == null || board[rank + 1][column].getColor() == Color.white) {
                        result[index++] = (rank + 1) * 10 + column;
                    }

                    if (board[rank][column - 1].getColor() == null || board[rank][column - 1].getColor() == Color.white) {
                        result[index++] = rank * 10 + column - 1;
                    }

                    if (board[rank - 1][column - 1].getColor() == null || board[rank - 1][column - 1].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column - 1;
                    }
                    if (board[rank - 1][column].getColor() == null || board[rank - 1][column].getColor() == Color.white) {
                        result[index++] = (rank - 1) * 10 + column;
                    }
                } else if (rank == 0 && column == 0) {
                    //check a2,b2,b1
                    if (board[0][1].getColor() == null || board[0][1].getColor() == Color.white) {
                        result[index++] = 1;
                    }
                    if (board[1][1].getColor() == null || board[1][1].getColor() == Color.white) {
                        result[index++] = 11;
                    }
                    if (board[1][0].getColor() == null || board[1][0].getColor() == Color.white) {
                        result[index++] = 10;
                    }
                } else if (rank == 7 && column == 0) {
                    //check a7,b8,b7
                    if (board[0][6].getColor() == null || board[0][6].getColor() == Color.white) {
                        result[index++] = 6;
                    }
                    if (board[1][6].getColor() == null || board[1][6].getColor() == Color.white) {
                        result[index++] = 16;
                    }
                    if (board[1][7].getColor() == null || board[1][7].getColor() == Color.white) {
                        result[index++] = 17;
                    }
                } else if (rank == 0 && column == 7) {
                    //check h2,g2,g1
                    if (board[7][1].getColor() == null || board[7][1].getColor() == Color.white) {
                        result[index++] = 71;
                    }
                    if (board[6][1].getColor() == null || board[6][1].getColor() == Color.white) {
                        result[index++] = 61;
                    }
                    if (board[6][0].getColor() == null || board[6][0].getColor() == Color.white) {
                        result[index++] = 60;
                    }
                } else if (rank == 7 && column == 7) {
                    //check h7,g7,g8
                    if (board[7][6].getColor() == null || board[7][6].getColor() == Color.white) {
                        result[index++] = 76;
                    }
                    if (board[6][6].getColor() == null || board[6][6].getColor() == Color.white) {
                        result[index++] = 66;
                    }
                    if (board[6][7].getColor() == null || board[6][7].getColor() == Color.white) {
                        result[index++] = 67;
                    }
                }
            } else if (board[rank][column].getColor() == Color.white) {
                if (!whiteKingMoved && !checked) {
                    //King side castle
                    if (board[0][5].getColor() == null && board[0][6].getColor() == null && !whiteHRookMoved) {
                        //f1 and g1 clear and H rook has not moved
                        result[index++] = 6;
                    }

                    //Queen side castle
                    if (board[0][3].getColor() == null && board[0][2].getColor() == null && board[0][1].getColor() == null && !whiteARookMoved) {
                        //d8, c8, b8 clear and A rook not has not moved
                        result[index++] = 2;
                    }
                }
                if (rank > 0 && column > 0 && rank < 7 && column < 7) {
                    //Check all sides
                    if (board[rank - 1][column + 1].getColor() == null || board[rank - 1][column + 1].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column + 1;
                    }
                    if (board[rank - 1][column].getColor() == null || board[rank - 1][column].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column;
                    }
                    if (board[rank - 1][column - 1].getColor() == null || board[rank - 1][column - 1].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column - 1;
                    }

                    if (board[rank][column + 1].getColor() == null || board[rank][column + 1].getColor() == Color.black) {
                        result[index++] = rank * 10 + column + 1;
                    }
                    if (board[rank][column - 1].getColor() == null || board[rank][column - 1].getColor() == Color.black) {
                        result[index++] = rank * 10 + column - 1;
                    }

                    if (board[rank + 1][column + 1].getColor() == null || board[rank + 1][column + 1].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column + 1;
                    }
                    if (board[rank + 1][column].getColor() == null || board[rank + 1][column].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column;
                    }
                    if (board[rank + 1][column - 1].getColor() == null || board[rank + 1][column - 1].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column - 1;
                    }
                } else if (rank == 0 && column > 0 && column < 7) {
                    //Check l,ul,u,ur,r
                    if (board[rank + 1][column + 1].getColor() == null || board[rank + 1][column + 1].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column + 1;
                    }
                    if (board[rank + 1][column].getColor() == null || board[rank + 1][column].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column;
                    }
                    if (board[rank + 1][column - 1].getColor() == null || board[rank + 1][column - 1].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column - 1;
                    }

                    if (board[rank][column + 1].getColor() == null || board[rank][column + 1].getColor() == Color.black) {
                        result[index++] = rank * 10 + column + 1;
                    }
                    if (board[rank][column - 1].getColor() == null || board[rank][column - 1].getColor() == Color.black) {
                        result[index++] = rank * 10 + column - 1;
                    }
                } else if (rank == 7 && column > 0 && column < 7) {
                    //check l, bl, b, br, r
                    if (board[rank - 1][column + 1].getColor() == null || board[rank - 1][column + 1].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column + 1;
                    }
                    if (board[rank - 1][column].getColor() == null || board[rank - 1][column].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column;
                    }
                    if (board[rank - 1][column - 1].getColor() == null || board[rank - 1][column - 1].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column - 1;
                    }

                    if (board[rank][column + 1].getColor() == null || board[rank][column + 1].getColor() == Color.black) {
                        result[index++] = rank * 10 + column + 1;
                    }
                    if (board[rank][column - 1].getColor() == null || board[rank][column - 1].getColor() == Color.black) {
                        result[index++] = rank * 10 + column - 1;
                    }
                } else if (column == 0 && rank > 0 && rank < 7) {
                    //check u, ur, r, br, b
                    if (board[rank + 1][column + 1].getColor() == null || board[rank + 1][column + 1].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column + 1;
                    }
                    if (board[rank + 1][column].getColor() == null || board[rank + 1][column].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column;
                    }

                    if (board[rank][column + 1].getColor() == null || board[rank][column + 1].getColor() == Color.black) {
                        result[index++] = rank * 10 + column + 1;
                    }

                    if (board[rank - 1][column + 1].getColor() == null || board[rank - 1][column + 1].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column + 1;
                    }
                    if (board[rank - 1][column].getColor() == null || board[rank - 1][column].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column;
                    }
                } else if (column == 7 && rank > 0 && rank < 7) {
                    //check u, ul, l, bl, b
                    if (board[rank + 1][column - 1].getColor() == null || board[rank + 1][column - 1].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column - 1;
                    }
                    if (board[rank + 1][column].getColor() == null || board[rank + 1][column].getColor() == Color.black) {
                        result[index++] = (rank + 1) * 10 + column;
                    }

                    if (board[rank][column - 1].getColor() == null || board[rank][column - 1].getColor() == Color.black) {
                        result[index++] = rank * 10 + column - 1;
                    }

                    if (board[rank - 1][column - 1].getColor() == null || board[rank - 1][column - 1].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column - 1;
                    }
                    if (board[rank - 1][column].getColor() == null || board[rank - 1][column].getColor() == Color.black) {
                        result[index++] = (rank - 1) * 10 + column;
                    }
                } else if (rank == 0 && column == 0) {
                    //check a2,b2,b1
                    if (board[0][1].getColor() == null || board[0][1].getColor() == Color.black) {
                        result[index++] = 1;
                    }
                    if (board[1][1].getColor() == null || board[1][1].getColor() == Color.black) {
                        result[index++] = 11;
                    }
                    if (board[1][0].getColor() == null || board[1][0].getColor() == Color.black) {
                        result[index++] = 10;
                    }
                } else if (rank == 7 && column == 0) {
                    //check a7,b8,b7
                    if (board[0][6].getColor() == null || board[0][6].getColor() == Color.black) {
                        result[index++] = 6;
                    }
                    if (board[1][6].getColor() == null || board[1][6].getColor() == Color.black) {
                        result[index++] = 16;
                    }
                    if (board[1][7].getColor() == null || board[1][7].getColor() == Color.black) {
                        result[index++] = 17;
                    }
                } else if (rank == 0 && column == 7) {
                    //check h2,g2,g1
                    if (board[7][1].getColor() == null || board[7][1].getColor() == Color.black) {
                        result[index++] = 71;
                    }
                    if (board[6][1].getColor() == null || board[6][1].getColor() == Color.black) {
                        result[index++] = 61;
                    }
                    if (board[6][0].getColor() == null || board[6][0].getColor() == Color.black) {
                        result[index++] = 60;
                    }
                } else if (rank == 7 && column == 7) {
                    //check h7,g7,g8
                    if (board[7][6].getColor() == null || board[7][6].getColor() == Color.black) {
                        result[index++] = 76;
                    }
                    if (board[6][6].getColor() == null || board[6][6].getColor() == Color.black) {
                        result[index++] = 66;
                    }
                    if (board[6][7].getColor() == null || board[6][7].getColor() == Color.black) {
                        result[index++] = 67;
                    }
                }
            }
            // </editor-fold>
        } else {
            // <editor-fold defaultstate="collapsed" desc="Pawn move logic">

            //start position
            if (rank == 1 && board[rank][column].getColor() == Color.white) {
                if (board[2][column].toString().equals(" ")) {
                    result[index++] = 20 + column;
                    if (board[3][column].toString().equals(" ")) {
                        result[index++] = 30 + column;
                    }
                }
                if (column > 0 && board[2][column - 1].getColor() == Color.black) {
                    result[index++] = 20 + column - 1;
                }
                if (column < 7 && board[2][column + 1].getColor() == Color.black) {
                    result[index++] = 20 + column + 1;
                }
            } else if (rank == 6 && board[rank][column].getColor() == Color.black) {
                if (board[5][column].toString().equals(" ")) {
                    result[index++] = 50 + column;
                    if (board[4][column].toString().equals(" ")) {
                        result[index++] = 40 + column;
                    }
                }
                if (column > 0 && board[5][column - 1].getColor() == Color.white) {
                    result[index++] = 50 + column - 1;
                }
                if (column < 7 && board[5][column + 1].getColor() == Color.white) {
                    result[index++] = 50 + column + 1;
                }
            }

            //single space forward after first move
            if (1 < rank && rank < 6) {
                if ((board[rank][column].getColor() == Color.black) && (board[rank - 1][column].toString().equals(" "))) {
                    result[index++] = (rank - 1) * 10 + column;
                } else if ((board[rank][column].getColor() == Color.white) && (board[rank + 1][column].toString().equals(" "))) {
                    result[index++] = (rank + 1) * 10 + column;
                }
            }

            //capture diagonal after first move
            if (rank > 1 && board[rank][column].getColor() == Color.black) {
                if (column > 0 && board[rank - 1][column - 1].getColor() == Color.white) {
                    result[index++] = (rank - 1) * 10 + column - 1;
                }
                if (column < 7 && board[rank - 1][column + 1].getColor() == Color.white) {
                    result[index++] = (rank - 1) * 10 + column + 1;
                }
            } else if (rank < 6 && board[rank][column].getColor() == Color.white) {
                if (column > 0 && board[rank + 1][column - 1].getColor() == Color.black) {
                    result[index++] = (rank + 1) * 10 + column - 1;
                }
                if (column < 7 && board[rank + 1][column + 1].getColor() == Color.black) {
                    result[index++] = (rank + 1) * 10 + column + 1;
                }
            }

            //promotion
            if (rank == 1 && board[rank][column].getColor() == Color.black) {
                if (board[0][column].getColor() == null) {
                    result[index++] = column;
                }
                if (column > 0) {
                    if (board[0][column - 1].getColor() == Color.white) {
                        result[index++] = column - 1;
                    }
                }
                if (column < 7) {
                    if (board[0][column + 1].getColor() == Color.white) {
                        result[index++] = column + 1;
                    }
                }
            } else if (rank == 6 && board[rank][column].getColor() == Color.white) {
                if (board[7][column].getColor() == null) {
                    result[index++] = 70 + column;
                }
                if (column > 0) {
                    if (board[7][column - 1].getColor() == Color.black) {
                        result[index++] = 70 + column - 1;
                    }
                }
                if (column < 7) {
                    if (board[7][column + 1].getColor() == Color.black) {
                        result[index++] = 70 + column + 1;
                    }
                }
            }

            if (lastMovePawn) {
                if (board[rank][column].getColor() == Color.white && rank == 4 && lastMove / 10 == 4 && (lastMove % 10 == column - 1 || lastMove % 10 == column + 1)) {
                    result[index++] = 50 + lastMove % 10;
                }
                if (board[rank][column].getColor() == Color.black && rank == 3 && lastMove / 10 == 3 && (lastMove % 10 == column - 1 || lastMove % 10 == column + 1)) {
                    result[index++] = 20 + lastMove % 10;
                }
            }
            // </editor-fold>
        }
        result = updateMoves(result, rank, column); //remove any moves which would give the opponent checkmate
        return result;
    }
    public boolean kingCheck(final boolean whiteTurn) {
        int r = 0;
        int c = 0;
        int kingRank = 0;
        int kingColumn = 0;
        boolean search = true;
        checked = false;

        //scan the board and find the opponent's king
        while (r <= 7 && search) {
            while (c <= 7 && search) {
                if ((board[r][c].toString().equals("K"))
                        && (((board[r][c].getColor() == Color.black) && whiteTurn)
                        || ((board[r][c].getColor() == Color.white) && !whiteTurn))) {
                    kingRank = r;
                    kingColumn = c;
                    search = false; //set the opponent's king's position, break both loops to stop searching
                }
                c++;
            }//end column loop
            c = 0;
            r++;
        }//end rank loop

        //if it's white's turn the found king will be black, if it's black's turn the found king will be white

        //look for check above
        if (kingRank < 7) {
            for (int x = kingRank + 1; x <= 7; x++) {
                if (board[x][kingColumn].getColor() != null) { //ran into another piece
                    if ((((board[x][kingColumn].getColor() == Color.black) && !whiteTurn) ||//black piece
                            ((board[x][kingColumn].getColor() == Color.white) && whiteTurn)) //white piece
                            && ((board[x][kingColumn].toString().equals("R")) || (board[x][kingColumn].toString().equals("Q")))) {
                        return true;
                    }
                    break; //blocked by another piece that doesn't put opponent in check
                }
            }//end for loop
        }//end conditional

        //check below
        if (kingRank > 0) {
            for (int x = kingRank - 1; x >= 0; x--) {
                if (board[x][kingColumn].getColor() != null) {
                    if ((((board[x][kingColumn].getColor() == Color.black) && !whiteTurn)
                            || ((board[x][kingColumn].getColor() == Color.white) && whiteTurn))
                            && ((board[x][kingColumn].toString().equals("R")) || (board[x][kingColumn].toString().equals("Q")))) {
                        return true;
                    }
                    break;
                }
            }//end for loop
        }//end conditional

        //check left
        if (kingColumn > 0) {
            for (int x = kingColumn - 1; x >= 0; x--) {
                if (board[kingRank][x].getColor() != null) {
                    if ((((board[kingRank][x].getColor() == Color.black) && !whiteTurn)
                            || ((board[kingRank][x].getColor() == Color.white) && whiteTurn))
                            && ((board[kingRank][x].toString().equals("R")) || (board[kingRank][x].toString().equals("Q")))) {
                        return true;
                    }
                    break;
                }
            }//end for loop
        }//end conditional

        //check right
        if (kingColumn < 7) {
            for (int x = kingColumn + 1; x <= 7; x++) {
                if (board[kingRank][x].getColor() != null) {
                    if ((((board[kingRank][x].getColor() == Color.black) && !whiteTurn)
                            || ((board[kingRank][x].getColor() == Color.white) && whiteTurn))
                            && ((board[kingRank][x].toString().equals("R")) || (board[kingRank][x].toString().equals("Q")))) {
                        return true;
                    }
                    break;
                }
            }//end for loop
        }//end conditional

        //look for check above-right
        r = kingRank + 1;
        c = kingColumn + 1;
        while (r <= 7 && c <= 7) {
            if (board[r][c].getColor() != null) {
                if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                        || ((board[r][c].getColor() == Color.white) && whiteTurn))
                        && ((board[r][c].toString().equals("B")) || (board[r][c].toString().equals("Q")))) {
                    return true;
                }
                break;
            }
            r++;
            c++;
        }
        //below-left
        r = kingRank - 1;
        c = kingColumn - 1;
        while (r >= 0 && c >= 0) {
            if (board[r][c].getColor() != null) {
                if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                        || ((board[r][c].getColor() == Color.white) && whiteTurn))
                        && ((board[r][c].toString().equals("B")) || (board[r][c].toString().equals("Q")))) {
                    return true;
                }
                break;
            }
            r--;
            c--;
        }
        //above-left
        r = kingRank + 1;
        c = kingColumn - 1;
        while (r <= 7 && c >= 0) {
            if (board[r][c].getColor() != null) {
                if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                        || ((board[r][c].getColor() == Color.white) && whiteTurn))
                        && ((board[r][c].toString().equals("B")) || (board[r][c].toString().equals("Q")))) {
                    return true;
                }
                break;
            }
            r++;
            c--;
        }
        //below-right
        r = kingRank - 1;
        c = kingColumn + 1;
        while (r >= 0 && c <= 7) {
            if (board[r][c].getColor() != null) {
                if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                        || ((board[r][c].getColor() == Color.white) && whiteTurn))
                        && ((board[r][c].toString().equals("B")) || (board[r][c].toString().equals("Q")))) {
                    return true;
                }
                break;
            }
            r--;
            c++;
        }

        //knights
        if (kingRank + 2 <= 7) {
            //up 2 right 1
            if (kingColumn + 1 <= 7) {
                if ((((board[kingRank + 2][kingColumn + 1].getColor() == Color.black) && !whiteTurn)
                        || ((board[kingRank + 2][kingColumn + 1].getColor() == Color.white) && whiteTurn))
                        && (board[kingRank + 2][kingColumn + 1].toString().equals("N"))) {
                    return true;
                }
            }
            //up 2 left 1
            if (kingColumn - 1 >= 0) {
                if ((((board[kingRank + 2][kingColumn - 1].getColor() == Color.black) && !whiteTurn)
                        || ((board[kingRank + 2][kingColumn - 1].getColor() == Color.white) && whiteTurn))
                        && (board[kingRank + 2][kingColumn - 1].toString().equals("N"))) {
                    return true;
                }
            }
        }
        if (kingColumn + 2 <= 7) {
            //right 2 up 1
            if (kingRank + 1 <= 7) {
                if ((((board[kingRank + 1][kingColumn + 2].getColor() == Color.black) && !whiteTurn)
                        || ((board[kingRank + 1][kingColumn + 2].getColor() == Color.white) && whiteTurn))
                        && (board[kingRank + 1][kingColumn + 2].toString().equals("N"))) {
                    return true;
                }
            }
            //right 2 down 1
            if (kingRank - 1 >= 0) {
                if ((((board[kingRank - 1][kingColumn + 2].getColor() == Color.black) && !whiteTurn)
                        || ((board[kingRank - 1][kingColumn + 2].getColor() == Color.white) && whiteTurn))
                        && (board[kingRank - 1][kingColumn + 2].toString().equals("N"))) {
                    return true;
                }
            }
        }
        if (kingRank - 2 >= 0) {
            //down 2 right 1
            if (kingColumn + 1 <= 7) {
                if ((((board[kingRank - 2][kingColumn + 1].getColor() == Color.black) && !whiteTurn)
                        || ((board[kingRank - 2][kingColumn + 1].getColor() == Color.white) && whiteTurn))
                        && (board[kingRank - 2][kingColumn + 1].toString().equals("N"))) {
                    return true;
                }
            }
            //down 2 left 1
            if (kingColumn - 1 >= 0) {
                if ((((board[kingRank - 2][kingColumn - 1].getColor() == Color.black) && !whiteTurn)
                        || ((board[kingRank - 2][kingColumn - 1].getColor() == Color.white) && whiteTurn))
                        && (board[kingRank - 2][kingColumn - 1].toString().equals("N"))) {
                    return true;
                }
            }
        }
        if (kingColumn - 2 >= 0) {
            //left 2 up 1
            if (kingRank + 1 <= 7) {
                if ((((board[kingRank + 1][kingColumn - 2].getColor() == Color.black) && !whiteTurn)
                        || ((board[kingRank + 1][kingColumn - 2].getColor() == Color.white) && whiteTurn))
                        && (board[kingRank + 1][kingColumn - 2].toString().equals("N"))) {
                    return true;
                }
            }
            //left 2 down 1
            if (kingRank - 1 >= 0) {
                if ((((board[kingRank - 1][kingColumn - 2].getColor() == Color.black) && !whiteTurn)
                        || ((board[kingRank - 1][kingColumn - 2].getColor() == Color.white) && whiteTurn))
                        && (board[kingRank - 1][kingColumn - 2].toString().equals("N"))) {
                    return true;
                }
            }
        }
        //pawns
        if (whiteTurn) {
            if (((kingColumn > 0 && kingRank > 0 && board[kingRank - 1][kingColumn - 1].getColor() == Color.white))
                    && (board[kingRank - 1][kingColumn - 1].toString().equals("P"))) {
                return true;
            }
            if (((kingColumn < 7 && kingRank > 0 && board[kingRank - 1][kingColumn + 1].getColor() == Color.white))
                    && (board[kingRank - 1][kingColumn + 1].toString().equals("P"))) {
                return true;
            }
        } else if (!whiteTurn) {
            if (((kingColumn > 0 && kingRank < 7 && board[kingRank + 1][kingColumn - 1].getColor() == Color.black))
                    && (board[kingRank + 1][kingColumn - 1].toString().equals("P"))) {
                return true;
            }
            if (((kingColumn < 7 && kingRank < 7 && board[kingRank + 1][kingColumn + 1].getColor() == Color.black))
                    && (board[kingRank + 1][kingColumn + 1].toString().equals("P"))) {
                return true;
            }
        }
        //prevent from moving king into checkmate by other king
        //up 1
        if (kingRank < 7) {
            if ((((board[kingRank + 1][kingColumn].getColor() == Color.black) && !whiteTurn)
                    || ((board[kingRank + 1][kingColumn].getColor() == Color.white) && whiteTurn))
                    && (board[kingRank + 1][kingColumn].toString().equals("K"))) {
                return true;
            }
        }
        //down 1
        if (kingRank > 0) {
            if ((((board[kingRank - 1][kingColumn].getColor() == Color.black) && !whiteTurn)
                    || ((board[kingRank - 1][kingColumn].getColor() == Color.white) && whiteTurn))
                    && (board[kingRank - 1][kingColumn].toString().equals("K"))) {
                return true;
            }
        }
        //left 1
        if (kingColumn > 0) {
            if ((((board[kingRank][kingColumn - 1].getColor() == Color.black) && !whiteTurn)
                    || ((board[kingRank][kingColumn - 1].getColor() == Color.white) && whiteTurn))
                    && (board[kingRank][kingColumn - 1].toString().equals("K"))) {
                return true;
            }
        }
        //right 1
        if (kingColumn < 7) {
            if ((((board[kingRank][kingColumn + 1].getColor() == Color.black) && !whiteTurn)
                    || ((board[kingRank][kingColumn + 1].getColor() == Color.white) && whiteTurn))
                    && (board[kingRank][kingColumn + 1].toString().equals("K"))) {
                return true;
            }
        }
        //left 1 down 1
        if ((kingRank > 0) && (kingColumn > 0)) {
            if ((((board[kingRank - 1][kingColumn - 1].getColor() == Color.black) && !whiteTurn)
                    || ((board[kingRank - 1][kingColumn - 1].getColor() == Color.white) && whiteTurn))
                    && (board[kingRank - 1][kingColumn - 1].toString().equals("K"))) {
                return true;
            }
        }
        //left 1 up 1
        if ((kingRank < 7) && (kingColumn > 0)) {
            if ((((board[kingRank + 1][kingColumn - 1].getColor() == Color.black) && !whiteTurn)
                    || ((board[kingRank + 1][kingColumn - 1].getColor() == Color.white) && whiteTurn))
                    && (board[kingRank + 1][kingColumn - 1].toString().equals("K"))) {
                return true;
            }
        }
        //right 1 down 1
        if ((kingRank > 0) && (kingColumn < 7)) {
            if ((((board[kingRank - 1][kingColumn + 1].getColor() == Color.black) && !whiteTurn)
                    || ((board[kingRank - 1][kingColumn + 1].getColor() == Color.white) && whiteTurn))
                    && (board[kingRank - 1][kingColumn + 1].toString().equals("K"))) {
                return true;
            }
        }
        //right 1 up 1
        if ((kingRank < 7) && (kingColumn < 7)) {
            if ((((board[kingRank + 1][kingColumn + 1].getColor() == Color.black) && !whiteTurn)
                    || ((board[kingRank + 1][kingColumn + 1].getColor() == Color.white) && whiteTurn))
                    && (board[kingRank + 1][kingColumn + 1].toString().equals("K"))) {
                return true;
            }
        }
        return false;
    }//end kingCheck
    private int[] updateMoves(int[] moves, final int sourceRank, final int sourceColumn) {
        final Piece limbo[][] = new Piece[1][1];
        boolean whiteTurn = false;
        if (board[sourceRank][sourceColumn].getColor() == Color.white) {
            whiteTurn = true;
        }

        //index through the list of moves.
        for (int x = 0; moves[x] != 99; x++) {	//prevent castling through check

            if ((board[sourceRank][sourceColumn].toString().equals("K")) && whiteTurn && !whiteKingMoved && moves[x] == 2)//castling via A1 valid
            {	//hypothetical move the piece
                board[0][3] = board[0][4];  //put a king here to check
                board[0][4] = new Empty();

                if (kingCheck(!whiteTurn)) {//undo hypothetical move
                    board[0][4] = board[0][3];
                    board[0][3] = new Empty();

                    for (int i = x; moves[i] != 99; i++) {//shift the moves array
                        moves[i] = moves[i + 1];
                    }
                    x--;
                } else { //undo hypothetical move
                    board[0][4] = board[0][3];
                    board[0][3] = new Empty();
                }
            } else if ((board[sourceRank][sourceColumn].toString().equals("K")) && whiteTurn && !whiteKingMoved && moves[x] == 6)//castling via H1 valid
            {
                board[0][5] = board[0][4];
                board[0][4] = new Empty();

                if (kingCheck(!whiteTurn)) {
                    board[0][4] = board[0][5];
                    board[0][5] = new Empty();

                    for (int i = x; moves[i] != 99; i++) {
                        moves[i] = moves[i + 1];
                    }
                    x--;
                } else {
                    board[0][4] = board[0][5];
                    board[0][5] = new Empty();
                }
            } else if ((board[sourceRank][sourceColumn].toString().equals("K")) && !whiteTurn && !blackKingMoved && moves[x] == 72)//castling via A8 valid
            {
                board[7][3] = board[7][4];
                board[7][4] = new Empty();

                if (kingCheck(!whiteTurn)) {
                    board[7][4] = board[7][3];
                    board[7][3] = new Empty();

                    for (int i = x; moves[i] != 99; i++) {
                        moves[i] = moves[i + 1];
                    }
                    x--;
                } else {
                    board[7][4] = board[7][3];
                    board[7][3] = new Empty();
                }
            } else if ((board[sourceRank][sourceColumn].toString().equals("K")) && !whiteTurn && !blackKingMoved && moves[x] == 76)//castling via H8 valid
            {
                board[7][5] = board[7][4];
                board[7][4] = new Empty();

                if (kingCheck(!whiteTurn)) {
                    board[7][4] = board[7][5];
                    board[7][5] = new Empty();

                    for (int i = x; moves[i] != 99; i++) {
                        moves[i] = moves[i + 1];
                    }
                    x--;
                } else { //undo hypothetical move
                    board[7][4] = board[7][5];
                    board[7][5] = new Empty();
                }
            }// end castling through check
            else { //hypothetical move the piece
                limbo[0][0] = board[moves[x] / 10][moves[x] % 10]; //move the space to limbo in case there's a piece that will be captured
                board[moves[x] / 10][moves[x] % 10] = board[sourceRank][sourceColumn];
                board[sourceRank][sourceColumn] = new Empty();

                if (kingCheck(!whiteTurn)) {//undo hypothetical move
                    board[sourceRank][sourceColumn] = board[moves[x] / 10][moves[x] % 10];
                    board[moves[x] / 10][moves[x] % 10] = limbo[0][0];

                    //shift the moves array
                    for (int i = x; moves[i] != 99; i++) {
                        moves[i] = moves[i + 1];
                    }
                    x--;
                } else {//undo hypothetical move
                    board[sourceRank][sourceColumn] = board[moves[x] / 10][moves[x] % 10];
                    board[moves[x] / 10][moves[x] % 10] = limbo[0][0];
                }
            }
        }//end loop
        return moves;
    }
    public boolean mateCheck(final boolean turn) {
        int rank = 0;
        int column = 0;
        boolean opponentPiece = false;

        while (rank <= 7) {
            while (column <= 7) {
                if ((turn && (board[rank][column].getColor() == Color.black)) || //white's turn, look for black pieces's valid moves
                        (!turn && (board[rank][column].getColor() == Color.white))) {
                    opponentPiece = true;
                } else {
                    opponentPiece = false;
                }
                //test the valid moves for each of the opponent's pieces
                if (opponentPiece && validMoves(rank, column)[0] != 99) {//there was at least one valid move which did not result in the checkmate
                    return false;
                }
                column++;
            }//end column loop
            column = 0;
            rank++;
        }//end rank loop
        return true; //made it all the way through without finding a valid move, return true indicating checkmate
    }//end matecheck
    //troubleshooter
    public String validString(int moves[], int rank, int column) {
        final StringBuilder returnString = new StringBuilder();
        returnString.append("piece:").append(rank).append(column).append("\n");
        for (int x = 0; x < 30; x++) {
            if (x < 10) {
                returnString.append("[0").append(x).append("]");
            } else {
                returnString.append("[").append(x).append("]");
            }
        }
        returnString.append("\n ");
        for (int x = 0; x < 30; x++) {
            returnString.append(moves[x]).append("  ");
        }
        returnString.append("\n");
        return returnString.toString();
    }
    private void threeFold() {
        if (hashCount == 49) {
            draw = true;
            return;
        }
        String hashbrown = boardHash();
        for (int x = 0; x < hashCount; x++) {
            if (threefold[x].equals(hashbrown)) {
                if (threefoldcount[x] < 3) {
                    threefoldcount[x]++;
                    if (threefoldcount[x] == 3) { //draw
                        draw = true;
                    } else {
                        return;
                    }
                }
            }
        }
        threefold[hashCount] = hashbrown;
        threefoldcount[hashCount] = 1;
        hashCount++;
        /*System.out.println("\n\n\n");
        for (int x = 0; x < hashCount; x++) {
        System.out.println(threefold[x] + " " + threefoldcount[x]);
        }*/
    }
    private void recordState() {//called from movePiece
        moveCounter++;
        boardState[moveCounter] = boardHash();//use for reverting the state AKA takeback
    }
    public void revertBoard(boolean castled) {//sets the logical board state to the previous board state essentially taking back the previous move
        String rState = "";
        int rCount = 0;
        if (castled && moveCounter > 1) {
            rState = boardState[moveCounter - 2];
        } else if (moveCounter > 0) {
            rState = boardState[moveCounter - 1];
        } else {
            return;
        }

        for (int r = MAX_ROWS - 1; r >= 0; r--) {
            for (int c = 0; c < MAX_COLUMNS; c++) {
                if (rState.charAt(rCount) == '0') {
                    board[r][c] = new Empty();
                } else if (rState.charAt(rCount) == 'p') {
                    board[r][c] = new Pawn(Color.black);
                } else if (rState.charAt(rCount) == 'P') {
                    board[r][c] = new Pawn(Color.white);
                } else if (rState.charAt(rCount) == 'r') {
                    board[r][c] = new Rook(Color.black);
                } else if (rState.charAt(rCount) == 'R') {
                    board[r][c] = new Rook(Color.white);
                } else if (rState.charAt(rCount) == 'b') {
                    board[r][c] = new Bishop(Color.black);
                } else if (rState.charAt(rCount) == 'B') {
                    board[r][c] = new Bishop(Color.white);
                } else if (rState.charAt(rCount) == 'n') {
                    board[r][c] = new Knight(Color.black);
                } else if (rState.charAt(rCount) == 'N') {
                    board[r][c] = new Knight(Color.white);
                } else if (rState.charAt(rCount) == 'q') {
                    board[r][c] = new Queen(Color.black);
                } else if (rState.charAt(rCount) == 'Q') {
                    board[r][c] = new Queen(Color.white);
                } else if (rState.charAt(rCount) == 'k') {
                    board[r][c] = new King(Color.black);
                } else if (rState.charAt(rCount) == 'K') {
                    board[r][c] = new King(Color.white);
                }
                rCount++;
            }
        }
        boardState[moveCounter] = null;
        moveCounter--;
        if (castled) {//back it up one more
            boardState[moveCounter] = null;
            moveCounter--;
        }
    }//end revert board
    public int[] boardChanges(boolean castled) {
        int[] changes = new int[5];//castling will cause the contents of max of four squares to change
        int index = 0;
        int rank = 0;
        int column = 0;
        String rState;

        for (int i = 0; i < 5; i++) {
            changes[i] = -1;
        }
        if (castled && moveCounter > 1) //castling happens in two seperate moves
        {
            rState = boardState[moveCounter - 2];//reverted board state prior to last move
        } else if (moveCounter > 0) {
            rState = boardState[moveCounter - 1];
        } else {
            return changes;
        }

        String cState = boardState[moveCounter];//current board state

        for (int x = 0; x < 64; x++) {//index through the two board states and put the changes into changes[]
            if (rState.charAt(x) != cState.charAt(x)) {
                //convert x to rank,column
                rank = 7 - (x / 8);
                column = (x % 8);

                if (castled) { //reset king/rook moved when castled by checking where the king was moved
                    if (rank == 0) {
                        whiteKingMoved = false;
                        if (column == 2) {
                            whiteARookMoved = false;
                        } else if (column == 6) {
                            whiteHRookMoved = false;
                        }
                    }
                    if (rank == 7) {
                        blackKingMoved = false;
                        if (column == 2) {
                            blackARookMoved = false;
                        } else if (column == 6) {
                            blackHRookMoved = false;
                        }
                    }
                }
                changes[index] = (rank * 10) + column;
                index++;
            }
        }
        return changes;
    }//end board changes
    public boolean multipleCheck(int rank, int column) {
        boolean whiteTurn = false;
        int r, c;
        if (board[rank][column].getColor() == Color.white) {
            whiteTurn = true;
        }

        if (board[rank][column].toString().equals("N")) {
            if (rank + 2 <= 7) {
                //up 2 right 1
                if (column + 1 <= 7) {
                    if ((((board[rank + 2][column + 1].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank + 2][column + 1].getColor() == Color.white) && whiteTurn))
                            && (board[rank + 2][column + 1].toString().equals("N"))) {
                        return true;
                    }
                }
                //up 2 left 1
                if (column - 1 >= 0) {
                    if ((((board[rank + 2][column - 1].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank + 2][column - 1].getColor() == Color.white) && whiteTurn))
                            && (board[rank + 2][column - 1].toString().equals("N"))) {
                        return true;
                    }
                }
            }
            if (column + 2 <= 7) {
                //right 2 up 1
                if (rank + 1 <= 7) {
                    if ((((board[rank + 1][column + 2].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank + 1][column + 2].getColor() == Color.white) && whiteTurn))
                            && (board[rank + 1][column + 2].toString().equals("N"))) {
                        return true;
                    }
                }
                //right 2 down 1
                if (rank - 1 >= 0) {
                    if ((((board[rank - 1][column + 2].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank - 1][column + 2].getColor() == Color.white) && whiteTurn))
                            && (board[rank - 1][column + 2].toString().equals("N"))) {
                        return true;
                    }
                }
            }
            if (rank - 2 >= 0) {
                //down 2 right 1
                if (column + 1 <= 7) {
                    if ((((board[rank - 2][column + 1].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank - 2][column + 1].getColor() == Color.white) && whiteTurn))
                            && (board[rank - 2][column + 1].toString().equals("N"))) {
                        return true;
                    }
                }
                //down 2 left 1
                if (column - 1 >= 0) {
                    if ((((board[rank - 2][column - 1].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank - 2][column - 1].getColor() == Color.white) && whiteTurn))
                            && (board[rank - 2][column - 1].toString().equals("N"))) {
                        return true;
                    }
                }
            }
            if (column - 2 >= 0) {
                //left 2 up 1
                if (rank + 1 <= 7) {
                    if ((((board[rank + 1][column - 2].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank + 1][column - 2].getColor() == Color.white) && whiteTurn))
                            && (board[rank + 1][column - 2].toString().equals("N"))) {
                        return true;
                    }
                }
                //left 2 down 1
                if (rank - 1 >= 0) {
                    if ((((board[rank - 1][column - 2].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank - 1][column - 2].getColor() == Color.white) && whiteTurn))
                            && (board[rank - 1][column - 2].toString().equals("N"))) {
                        return true;
                    }
                }
            }
        } else if (board[rank][column].toString().equals("R")) {//rook
            //look for rook above
            for (int x = rank + 1; x <= 7; x++) {
                if (board[x][column].getColor() != null) { //ran into another piece
                    if ((((board[x][column].getColor() == Color.black) && !whiteTurn) ||//black piece
                            ((board[x][column].getColor() == Color.white) && whiteTurn)) //white piece
                            && ((board[x][column].toString().equals("R")))) {//found the other rook
                        return true;
                    }
                    break; //the piece I ran into isn't the other rook, get out
                }
            }
            //check below
            for (int x = rank - 1; x >= 0; x--) {
                if (board[x][column].getColor() != null) {
                    if ((((board[x][column].getColor() == Color.black) && !whiteTurn)
                            || ((board[x][column].getColor() == Color.white) && whiteTurn))
                            && ((board[x][column].toString().equals("R")))) {
                        return true;
                    }
                    break;
                }
            }
            //check left
            for (int x = column - 1; x >= 0; x--) {
                if (board[rank][x].getColor() != null) {
                    if ((((board[rank][x].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank][x].getColor() == Color.white) && whiteTurn))
                            && ((board[rank][x].toString().equals("R")))) {
                        return true;
                    }
                    break;
                }
            }
            //check right
            for (int x = column + 1; x <= 7; x++) {
                if (board[rank][x].getColor() != null) {
                    if ((((board[rank][x].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank][x].getColor() == Color.white) && whiteTurn))
                            && ((board[rank][x].toString().equals("R")))) {
                        return true;
                    }
                    break;
                }
            }//end rook
        } else if (board[rank][column].toString().equals("B")) {
            //look for another bishop above right
            r = rank + 1;
            c = column + 1;
            while (r <= 7 && c <= 7) {
                if (board[r][c].getColor() != null) {
                    if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                            || ((board[r][c].getColor() == Color.white) && whiteTurn))
                            && (board[r][c].toString().equals("B"))) {
                        return true;
                    }
                    break;
                }
                r++;
                c++;
            }
            //below-left
            r = rank - 1;
            c = column - 1;
            while (r >= 0 && c >= 0) {
                if (board[r][c].getColor() != null) {
                    if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                            || ((board[r][c].getColor() == Color.white) && whiteTurn))
                            && (board[r][c].toString().equals("B"))) {
                        return true;
                    }
                    break;
                }
                r--;
                c--;
            }
            //above-left
            r = rank + 1;
            c = column - 1;
            while (r <= 7 && c >= 0) {
                if (board[r][c].getColor() != null) {
                    if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                            || ((board[r][c].getColor() == Color.white) && whiteTurn))
                            && (board[r][c].toString().equals("B"))) {
                        return true;
                    }
                    break;
                }
                r++;
                c--;
            }
            //below-right
            r = rank - 1;
            c = column + 1;
            while (r >= 0 && c <= 7) {
                if (board[r][c].getColor() != null) {
                    if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                            || ((board[r][c].getColor() == Color.white) && whiteTurn))
                            && (board[r][c].toString().equals("B"))) {
                        return true;
                    }
                    break;
                }
                r--;
                c++;
            }
        }//end bishop
        else if (board[rank][column].toString().equals("Q")) {//queen
            //look for queen above
            for (int x = rank + 1; x <= 7; x++) {
                if (board[x][column].getColor() != null) { //ran into another piece
                    if ((((board[x][column].getColor() == Color.black) && !whiteTurn) ||//black piece
                            ((board[x][column].getColor() == Color.white) && whiteTurn)) //white piece
                            && ((board[x][column].toString().equals("Q")))) {//found the other rook
                        return true;
                    }
                    break; //the piece I ran into isn't the other rook, get out
                }
            }
            //check below
            for (int x = rank - 1; x >= 0; x--) {
                if (board[x][column].getColor() != null) {
                    if ((((board[x][column].getColor() == Color.black) && !whiteTurn)
                            || ((board[x][column].getColor() == Color.white) && whiteTurn))
                            && ((board[x][column].toString().equals("Q")))) {
                        return true;
                    }
                    break;
                }
            }
            //check left
            for (int x = column - 1; x >= 0; x--) {
                if (board[rank][x].getColor() != null) {
                    if ((((board[rank][x].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank][x].getColor() == Color.white) && whiteTurn))
                            && ((board[rank][x].toString().equals("Q")))) {
                        return true;
                    }
                    break;
                }
            }
            //check right
            for (int x = column + 1; x <= 7; x++) {
                if (board[rank][x].getColor() != null) {
                    if ((((board[rank][x].getColor() == Color.black) && !whiteTurn)
                            || ((board[rank][x].getColor() == Color.white) && whiteTurn))
                            && ((board[rank][x].toString().equals("Q")))) {
                        return true;
                    }
                    break;
                }
            }

            //look for another queen above right
            r = rank + 1;
            c = column + 1;
            while (r <= 7 && c <= 7) {
                if (board[r][c].getColor() != null) {
                    if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                            || ((board[r][c].getColor() == Color.white) && whiteTurn))
                            && (board[r][c].toString().equals("Q"))) {
                        return true;
                    }
                    break;
                }
                r++;
                c++;
            }
            //below-left
            r = rank - 1;
            c = column - 1;
            while (r >= 0 && c >= 0) {
                if (board[r][c].getColor() != null) {
                    if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                            || ((board[r][c].getColor() == Color.white) && whiteTurn))
                            && (board[r][c].toString().equals("Q"))) {
                        return true;
                    }
                    break;
                }
                r--;
                c--;
            }
            //above-left
            r = rank + 1;
            c = column - 1;
            while (r <= 7 && c >= 0) {
                if (board[r][c].getColor() != null) {
                    if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                            || ((board[r][c].getColor() == Color.white) && whiteTurn))
                            && (board[r][c].toString().equals("Q"))) {
                        return true;
                    }
                    break;
                }
                r++;
                c--;
            }
            //below-right
            r = rank - 1;
            c = column + 1;
            while (r >= 0 && c <= 7) {
                if (board[r][c].getColor() != null) {
                    if ((((board[r][c].getColor() == Color.black) && !whiteTurn)
                            || ((board[r][c].getColor() == Color.white) && whiteTurn))
                            && (board[r][c].toString().equals("Q"))) {
                        return true;
                    }
                    break;
                }
                r--;
                c++;
            }
        }//end queen
        return false;
    }
    public String boardState()
    {
    	return this.boardHash();
    }
    public void convertHash (String hash){
    	System.out.println(hash);
    	int r=0;
    	int c=0;
    	for (int i = 0; i < 64; i++){
        	r = 7-(i/8);
    		c = i%8;
    		switch (hash.charAt(i)){
	            case '0':
	            	board[r][c] = new Empty();
	                break;
	            case 'p':
	            	board[r][c] = new Pawn(Color.black);
	                break;
	            case 'P':
	            	board[r][c] = new Pawn(Color.white);
	                break;
	            case 'n':
	            	board[r][c] = new Knight(Color.black);
	                break;
	            case 'N':
	            	board[r][c] = new Knight(Color.white);
	            	break;	
                case 'r':
	            	board[r][c] = new Rook(Color.black);
	                break;
	            case 'R':
	            	board[r][c] = new Rook(Color.white);
	                break;
	            case 'b':
	            	board[r][c] = new Bishop(Color.black);
	                break;
	            case 'B':
	            	board[r][c] = new Bishop(Color.white);
	                break;
	            case 'q':
	            	board[r][c] = new Queen(Color.black);
	                break;
	            case 'Q':
	            	board[r][c] = new Queen(Color.white);
	                break;
	            case 'k':
	            	board[r][c] = new King(Color.black);
	                break;
	            case 'K':
	            	board[r][c] = new King(Color.white);
	                break;
	            default:
	            	break;
    		}//end switch
    	}//end for loop
    	recordState();
		System.out.println(toString());
    }//end convert hash
    public void setLastMove(int move){
    	lastMove = move;
    }
    public String getLastMove(){
    	if (lastMove < 10){
    		return ("0" + lastMove);
    	} else {
    	return ("" + lastMove);
    	}
    }
    
    private static final int MAX_ROWS = 8;
    private static final int MAX_COLUMNS = 8;
    private boolean lastMovePawn;
    private int lastMove;
    private boolean whiteKingMoved, whiteARookMoved, whiteHRookMoved;
    private boolean blackKingMoved, blackARookMoved, blackHRookMoved;
    public static Piece board[][] = new Piece[MAX_ROWS][MAX_COLUMNS];  //use this for diagnostic, use other for final release
    public boolean checked;
    public boolean draw = false;
    private int hashCount = 1;
    private String[] threefold = new String[50];
    private int[] threefoldcount = new int[50];
    public String[] boardState = new String[100];
    private int moveCounter = 0;
}
